//package cn.flying.cloud.esb.core.camel.process;
//
//import javax.annotation.Resource;
//
//import java.util.Map;
//
//import org.apache.camel.Exchange;
//import org.apache.camel.Processor;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//
//import cn.flying.cloud.base.common.utils.Ft;
//import cn.flying.cloud.esb.core.camel.constants.CamelConstant;
//import cn.flying.cloud.esb.core.enums.ErrorCodeEnum;
//import cn.flying.cloud.esb.core.exception.EsbException;
//import cn.flying.cloud.esb.entity.EbApplicationInfo;
//import cn.flying.cloud.esb.entity.EbSubscribeService;
//import cn.flying.cloud.esb.model.ApiHead;
//import cn.flying.cloud.esb.model.ApiReqData;
//import cn.flying.cloud.esb.model.ApiRequest;
//import cn.flying.cloud.esb.service.EbApplicationInfoService;
//import cn.flying.cloud.esb.service.EbSubscribeServiceService;
//
///**
// * 参数校验处理器
// *
// * @author: admin
// * @date: 2025年04月10日 17:12
// * @version: 1.0
// */
//@Component
//public class ParamValidProcessor implements Processor {
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    @Resource
//    private EbApplicationInfoService applicationInfoService;
//    @Resource
//    private EbSubscribeServiceService applicationServiceService;
//
//    /**
//     * Processes the message exchange
//     *
//     * @param exchange the message exchange
//     * @throws Exception if an internal processing error has occurred.
//     */
//    @Override
//    public void process(Exchange exchange) throws Exception {
//        ApiRequest request = exchange.getProperty(CamelConstant.API_REQUEST, ApiRequest.class);
//        ApiReqData<?> apiReqData = request.getRequest();
//        if (apiReqData == null) {
//            throw new EsbException(ErrorCodeEnum.E_1004);
//        }
//        ApiHead head = apiReqData.getHead();
//        logger.info("检查参数合法，appId：{}，appKey：{}，serviceCode：{}，流水号：{}", head.getAppId(), head.getAppKey(), head.getServiceCode(), head.getSerialNo());
//        Map<String, String> map = Ft.Valid.valid(apiReqData);
//        if (!map.isEmpty()) {
//            throw new EsbException(ErrorCodeEnum.E_1004.getCode(), Ft.JSON.serializer(map));
//        }
//
//        logger.info("检查访问权限，appId：{}，appKey：{}，serviceCode：{}，流水号：{}", head.getAppId(), head.getAppKey(), head.getServiceCode(), head.getSerialNo());
//        EbApplicationInfo applicationInfo = new EbApplicationInfo();
//        applicationInfo.setAppId(head.getAppId());
//        applicationInfo.setAppKey(head.getAppKey());
//        applicationInfo = applicationInfoService.getOneObj(applicationInfo);
//        if (applicationInfo == null) {
//            // 应用标识符不存在
//            throw new EsbException(ErrorCodeEnum.E_1002);
//        }
//        EbSubscribeService applicationService = new EbSubscribeService();
//        applicationService.setAppId(head.getAppId());
////        applicationService.setAppKey(head.getAppKey());
//        applicationService.setServiceCode(head.getServiceCode());
//        applicationService = applicationServiceService.getOneObj(applicationService);
//        if (applicationService == null) {
//            // 没有访问接口服务权限
//            throw new EsbException(ErrorCodeEnum.E_1003);
//        }
//
//        logger.info("检查请求效期，appId：{}，appKey：{}，serviceCode：{}，流水号：{}", head.getAppId(), head.getAppKey(), head.getServiceCode(), head.getSerialNo());
//        String reqTime = head.getReqTime();
//        long time = Ft.Date.parseDate(reqTime, "yyyy-MM-dd HH:mm:ss").getTime();
//        // 请求5分钟内有效
//        if (time > time + 5 * 60 * 1000) {
//            throw new EsbException(ErrorCodeEnum.E_1005);
//        }
//
//        logger.info("检查数据签名，appId：{}，appKey：{}，serviceCode：{}，流水号：{}", head.getAppId(), head.getAppKey(), head.getServiceCode(), head.getSerialNo());
//        String signature = apiReqData.getSign();
//        if (signature == null || !validSignature(request, applicationInfo)) { // 实现你的校验逻辑
//            throw new EsbException(ErrorCodeEnum.E_1006);
//        }
//    }
//
//
//    private boolean validSignature(ApiRequest request, EbApplicationInfo applicationInfo) {
//        Map map = (Map) request.getRequest().getBody();
//        String str = "";
//        if (!map.isEmpty()) {
//            str = Ft.JSON.serializer(map, true);
//        }
//        // 校验签名的逻辑
////        try {
////            Ft.SM2.verify(str, applicationInfo.getAppPubKey(), request.getRequest().getSign());
////            return true;
////        } catch (Exception e) {
////            logger.error("验签失败", e);
////            return false;
////        }
//        return false;
//    }
//}
